/**
 *
 */
//let ;
Vue.component('treeselect', VueTreeselect.Treeselect);
var vm = new Vue({
    el:'#vm',
    data:{
        tableData:[],
        currentPage:1,
        count:0,
        title:undefined,
        dialogFormVisible: false,
        statusOptions:[],
        dictTypeStatus:'sys_status',
        dictTypeMenu:'sys_menu_type',
        formLabelWidth: '80px',
        respForm:{},
        dictList:undefined,
        form:{
            name:undefined,
            status:undefined,
            page:1,
            limit:10,
        },
        rules: {
            name: [{ required: true, message: '请输入菜单名', trigger: 'blur' }]
            ,parentId: [{ required: true, message: '请选择上级菜单', trigger: 'blur' }]
            ,type: [{ required: true, message: '请选择类型', trigger: 'blur' }]
            ,perms: [{ required: true, message: '请输入权限标识', trigger: 'blur' }]
            ,url: [{required: true, message: '请输入路径', trigger: ['blur'] }]
        }
    },
    created:function (){
        this.getDictList();

    },
    mounted:function(){
        this.getStatus();
    },
    methods:{
         getDictList(){
             axios.get('/sys/dict/getDictList').then((res)=> {
                this.dictList = res.data;
                $.ajax({
                    url:'/sys/dept/list',
                    type:'get',
                    dataType:"json",
                    data:{
                        data:JSON.stringify(this.form)
                    },
                    success:(res)=>{
                        res.data.forEach((item) => {
                            for( let key in this.dictList){
                                if (this.dictTypeStatus+"_"+item.status == key){
                                    item.status=this.dictList[key];
                                }else if (item.children!=null){
                                    this.screenData(item.children)
                                }
                            }
                        })
                        this.count = res.count;
                        this.tableData=res.data;
                    }
                })
            });
        },
        screenData(item){
            for(let i = 0; i < item.length; i++) {
                for( let key in this.dictList){
                    if (this.dictTypeStatus+"_"+item[i].status == key){
                        item[i].status=this.dictList[key];
                    }else if (item[i].children !=null){
                        this.screenData(item[i].children);
                    }
                }
            }
        },
        normalizer(node){
            return {
                id: node.deptId,
                label: node.name,
                children: node.children,
            }
        },
        getStatus(){
            axios.get('/sys/dict/getDictByType',{
                params:{
                    dictType:this.dictTypeStatus
                }
            }).then(res=> {
                this.statusOptions = res.data.data;
            })
        },
        statusChange(){
            this.getDictList();
        },
        resetForm(){
            this.respForm={
                deptId: undefined,
                parentId: undefined,
                name: undefined,
                leader: undefined,
                mobile: undefined,
                email: undefined,
                status: '0',
                sort: undefined,
            }
        },
        edit(row){
            this.resetForm();
            this.dialogFormVisible = true;
            this.getStatus();
            this.title="修改部门";
            axios.get('/sys/dept/info',{
                params:{
                    deptId:row.deptId
                }
            }).then(res=> {
                this.respForm = res.data.data;
            })
        },
        add(row){
            this.resetForm();
            this.dialogFormVisible = true;
            this.respForm.parentId=row.deptId;
            this.getStatus();
            this.title="添加部门";
        },
        del(row){
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                $.ajax({
                    url:'/sys/dept/del',
                    dataType:'json',
                    traditional:true,
                    type:'delete',
                    data:{
                        deptId:row.deptId
                    },
                    success:res=>{
                        if (res.code===200){
                            this.$notify({title:'成功',message:res.msg,type:'success'});
                            this.getDictList();
                        } else{
                            this.$notify({title:'警告',message:res.msg,type:'warning'});
                        }
                    }
                })
            }).catch(() => {
                this.$notify({type: 'info', message: '已取消删除',title:"提示"});
            });
        },
        search(){
            this.getDictList();
        },
        reset(form1){
            this.$refs[form1].resetFields()
        },
        submitForm(val){//提交
            this.$refs[val].validate(valid => {
                if(valid){
                    if(this.respForm.deptId === undefined){
                        axios.post('/sys/dept/add', this.respForm)
                            .then(res => {
                                if(res.status===200){
                                    this.$notify({ title: '成功',message: res.data.msg,type: 'success' });
                                    this.dialogFormVisible = false;
                                    this.getDictList();
                                }else{
                                    this.$notify({title: '警告',message:res.data.msg,type: 'warning'});
                                }
                            })
                    }else{
                        axios.put('/sys/dept/modify', this.respForm)
                            .then(res => {
                                if(res.status===200){
                                    this.$notify({ title: '成功',message: res.data.msg,type: 'success' });
                                    this.dialogFormVisible = false;
                                    this.getDictList();
                                }else{
                                    this.$notify({title: '警告',message:res.data.msg,type: 'warning'});
                                }
                            })
                    }
                }
            });
        }
    }
})

